package com.example.demo.Service;

import com.example.demo.model.RegionStats;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
@Component
@EnableScheduling
public class paw implements InitializingBean {

    public static List<RegionStats> CoronaVirusData;
    static {
        CoronaVirusData=new ArrayList<>();
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        CoronaVirusData= paw.getdata();
        System.out.println("数据更新");
    }
    private static RestTemplate restTemplate=new RestTemplate();
    private final static String uri ="https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
    //爬取全球新冠肺炎病例数据

    public static InputStream getbody() throws IOException {
        RequestEntity<Void> requestEntity = RequestEntity
                .get(URI.create(uri))
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "cxh"))
                .build();
        ResponseEntity<Resource> exchange = restTemplate.exchange(requestEntity, Resource.class);
        Resource body = exchange.getBody();
        InputStream inputStream =body.getInputStream();
        return inputStream;
    }

    public static List<RegionStats>getdata() throws IOException {
        List<RegionStats>all=new ArrayList<> ();
        InputStream date=getbody();
        Reader in = new InputStreamReader(date);
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader()
                .withDelimiter(',')
                .withQuote('"')
                .withRecordSeparator("\r\n")
                .withIgnoreEmptyLines(true)
                .withAllowDuplicateHeaderNames(true)
                .parse(in);
        for  (CSVRecord record : records) {
            String state = record.get("Province/State");
            String country = record.get("Country/Region");
            String latestTotalCases = record.get(record.size() - 1);
            String per = record.get(record.size() - 2);
            int t=Integer.parseInt(latestTotalCases);
            int p=Integer.parseInt(per);
            int a=t-p;
            RegionStats mo =new RegionStats();
            mo.setCountry(country);
            mo.setState(state);
            mo.setLatestTotalCases(t);
            mo.setDiffFromPrevDay(a);
            all.add(mo);
        }
        return  all;
    }
}
